#include "symboltable.h"
#include "Util.h"

string outputstr;

SymbolTable::SymbolTable()
{
    index = 0;
    paramcount = 0;
}

void SymbolTable::addRecord(string name, string kind, string type, Token *tk, SymbolTable *next)
{
    for (int i = 0; i < index; i++)
    {
        if (name == table[i]->name)
        {
            string temp = "Line ";
            temp += to_string(tk->line);
            temp += " ";
            temp += "\"";
            temp += name;
            temp += "\" ";
            temp += "RepeatDeclaration Error!\n";
            printErrorMsg(temp);
            return;
        }
    }
    table[index] = new SymbolRecord();
    char temp[100];

    table[index]->name = name;
    table[index]->kind = kind;
    table[index]->type = type;
    table[index]->tk = tk;
    table[index]->next = next;
    index++;
}

void SymbolTable::printTable(int layer)
{
    for (int i = 0; i < index; i++)
    {
        for (int i = 0; i < layer; i++)
        {
            std::cout << "\t\t\t\t\t";
            outputstr += "\t\t\t\t\t";
        }

        std::cout << table[i]->name
             << "\t"
             << table[i]->kind
             << "\t"
             << table[i]->type << endl;

        outputstr += table[i]->name;
        outputstr += "\t";
        outputstr += table[i]->kind;
        outputstr += "\t";
        outputstr += table[i]->type;
        outputstr += "\n";

        if (table[i]->next)
        {
            //cout << endl;
            //outputstr += "\n";
            table[i]->next->printTable(layer + 1);
            std::cout << endl;
            outputstr += "\n";
        }
    }
}
